<?php
function SMACalc($start, $end, $days, $Symbol){
require_once("DatabaseConnect.php");
//database cnnection
$con=Remoteconnect();
if($con==null){
	$con=Localconnect();	
}
//default value for days variable for when it is == to none
if($days=="none"){
$days=1;	
}
$SMAarray=array();
$prices= array();
$sum=0;
//2005-01-04
//2014-01-04
$index=0;
$count=0;
$num=0;
$dateArray=array();


$movingAvGstartDate="";

//get new start date: which is equal to startDate-days, inorder to obtain the correct moving day average for the users start date
$sql="SELECT * FROM `shares` WHERE `Symbol`='$Symbol' AND `Date`<'$start' Order by `Date` DESC Limit $days";

$movingAvGstartDateQuery=mysqli_query($con,$sql);
//iterate through the sql query, the last result is the date that we are looking to obtain
while($row=mysqli_fetch_array($movingAvGstartDateQuery)){
	$movingAvGstartDate=$row['Date'];
	
}



$range = mysqli_query($con,"SELECT * 
FROM  `shares` 
WHERE  `Symbol` =  '$Symbol'
AND  `Date` 
BETWEEN  '$movingAvGstartDate'
AND  '$end'
ORDER BY `Date` Asc");




while($row = mysqli_fetch_array($range)) {
	
	$count+=1;
	//echo"$count<br>";
	array_push($prices, $row['Close']);
	
	if($count>=$days){
		
		foreach($prices as $price){
			
			$num+=1;
			//echo"prices number: $num<br>";
			$sum+=$price;
		}
		
		$num=0;
		$avg=$sum/$days;
		//echo"$avg<br>";
		array_push($dateArray, $row['Date']);
		array_push($SMAarray, $avg);
		unset($prices[$index]);
		$sum=0;
		$index+=1;
}
  
}
	return $SMAarray;
}

function DateTestSMA($start, $end, $days, $Symbol){
	
	require_once("DatabaseConnect.php");
//database cnnection
$con=Remoteconnect();
if($con==null){
	$con=Localconnect();	
}
$SMAarray=array();
$prices= array();
$sum=0;
//2005-01-04
//2014-01-04
$index=0;
$count=0;
$num=0;
$dateArray=array();


$movingAvGstartDate="";

//get new start date: which is equal to startDate-days, inorder to obtain the correct moving day average for the users start date
$sql="SELECT * FROM `shares` WHERE `Symbol`='$Symbol' AND `Date`<'$start' Order by `Date` DESC Limit $days";

$movingAvGstartDateQuery=mysqli_query($con,$sql);
//iterate through the sql query, the last result is the date that we are looking to obtain
while($row=mysqli_fetch_array($movingAvGstartDateQuery)){
	$movingAvGstartDate=$row['Date'];
	
}



$range = mysqli_query($con,"SELECT * 
FROM  `shares` 
WHERE  `Symbol` =  '$Symbol'
AND  `Date` 
BETWEEN  '$movingAvGstartDate'
AND  '$end'
ORDER BY `Date` Asc");




while($row = mysqli_fetch_array($range)) {
	
	$count+=1;
	//echo"$count<br>";
	array_push($prices, $row['Close']);
	
	if($count>=$days){
		
		foreach($prices as $price){
			
			$num+=1;
			//echo"prices number: $num<br>";
			$sum+=$price;
		}
		
		$num=0;
		$avg=$sum/$days;
		//echo"$avg<br>";
		array_push($dateArray, $row['Date']);
		array_push($SMAarray, $avg);
		unset($prices[$index]);
		$sum=0;
		$index+=1;
}
  
}
	return $dateArray;
}
	
	
	



?>